package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TCompAgreerateDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCompAgreerate;

/**
 * 协议价维护Service
 * @author wuhao
 * @version 2016-08-08
 */
@Service
@Transactional(readOnly = true)
public class TCompAgreerateService extends CrudService<TCompAgreerateDao, TCompAgreerate> {

	public TCompAgreerate get(String agreerateid) {
		return super.get(agreerateid);
	}
	
	public List<TCompAgreerate> findList(TCompAgreerate tCompAgreerate) {
		if(tCompAgreerate.getOffice() == null){
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tCompAgreerate.setOffice(office);
		}
		return super.findList(tCompAgreerate);
	}
	
	public Page<TCompAgreerate> findPage(Page<TCompAgreerate> page, TCompAgreerate tCompAgreerate) {
		if(tCompAgreerate.getOffice() == null){
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tCompAgreerate.setOffice(office);
		}
		return super.findPage(page, tCompAgreerate);
	}
	
	@Transactional(readOnly = false)
	public void save(TCompAgreerate tCompAgreerate) {
		if(tCompAgreerate.getAgreerateid() == null) {
			User user = UserUtils.getUser();
			// 设置部门
			Office office = user.getOffice();
			tCompAgreerate.setOffice(office);
			tCompAgreerate.setCreateBy(user);
			tCompAgreerate.setCreateDate(new Date());
			dao.insert(tCompAgreerate);
		} else {
			dao.update(tCompAgreerate);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(TCompAgreerate tCompAgreerate) {
		super.delete(tCompAgreerate);
	}
	
}